package com.junyuan.gsp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: maxwellens
 */
public class DateUtil
{
    public static Date addDays(Date date, Integer days)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static Date addHours(Date date,Integer hours)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hours);
        return cal.getTime();
    }

    public static String toDate(Date date)
    {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static String toDateTime(Date date)
    {
        return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
    }

    public static Date parseDate(String date)
    {
        try
        {
            return new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e)
        {
            return null;
        }
    }

    public static Date parseDateTime(String date)
    {
        try
        {
            return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date);
        } catch (ParseException e)
        {
            return null;
        }
    }

}
